import { createVitePlugins } from './config/vite/plugins';
import { resolve } from 'path';
import { ConfigEnv, UserConfigExport } from 'vite';
const CopyWebpackPlugin = require("copy-webpack-plugin") //引入插件

const pathResolve = (dir: string) => {
  return resolve(process.cwd(), '.', dir);
};

// https://vitejs.dev/config/
export default function ({ command }: ConfigEnv): UserConfigExport {
  const isProduction = command === 'build';
  const root = process.cwd();
  return {
    root,
    resolve: {
      alias: [
        {
          find: 'vue-i18n',
          replacement: 'vue-i18n/dist/vue-i18n.cjs.js',
        },
        // /@/xxxx => src/xxxx
        {
          find: /\/@\//,
          replacement: pathResolve('src') + '/',
        },
        // /#/xxxx => types/xxxx
        {
          find: /\/#\//,
          replacement: pathResolve('types') + '/',
        },
      ],
    },
    server: {
      host: true,
      hmr: true,
      proxy: {
        '/hbmy_wx': {
          //部署需要改 todo
          // target: 'http://hegang.natapp1.cc/hbmy_wx/',
          target: 'https://proxy.hbmzu.edu.cn/hbmy_wx/',
          secure: false,
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/hbmy_wx/, '')
        },
        '/filepath': {
          //部署需要改 todo
          // target: 'http://hegang.natapp1.cc/filepath/',
          target: 'https://proxy.hbmzu.edu.cn/',
          secure: false,
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/filepath/, '')
        },
        '/jwfilepath': {
          //部署需要改 todo
          // target: 'http://hegang.natapp1.cc/filepath/',
          target: 'https://proxy.hbmzu.edu.cn/jwfilepath/',
          secure: false,
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/jwfilepath/, '')
        }
      }
    },
    plugins: createVitePlugins(isProduction),
    // todo
    // base: '/sportWx/',
    build: {
      minify: 'terser',
      terserOptions: {
        compress: {
          //生产环境时移除console
          drop_console: true,
          // drop_console: false,
          drop_debugger: true,
        },
      },
      // publicPath: '/sportWx/',
      // assetsPublicPath: '/sportWx/'

      // outDir: 'sportWx',
      // assetsDir: '././',
      // publicPath: '../',
      // assetsPublicPath: '../'
    },
    css: {
      preprocessorOptions: {
        scss: {
          // 配置 nutui 全局 scss 变量
          additionalData: `@import "@nutui/nutui/dist/styles/variables.scss";@import '/@/styles/mixin.scss';`,
        },
      },
    },
    configureWebpack: {
      plugins: [
        new CopyWebpackPlugin({
          patterns:[{
            from: "./public",
            to: 'public'
          }]
        })
      ]
    }
  };
}
